package com.bsh.serviceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bsh.api.ApiUtils;
import com.bsh.api.Exception.ApiException;
import com.bsh.mybatis.model.User;
import com.bsh.service.AccountNoticeService;
import com.bsh.service.AccountService;
import com.bsh.service.ApiBillService;
import com.bsh.service.SmsService;
import com.bsh.service.UserService;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.Des3;
import com.bsh.utils.Pager;

@Service
public class ApiBillServiceImpl implements ApiBillService {

    @Autowired
    UserService servUser;
//    @Autowired
//   // SmsService servSms;
    @Autowired
    AccountService accountServ;
    @Autowired
    AccountNoticeService veriService;

    @Override
    public ApiMessage billListMoney(JSONObject dto) throws ApiException {
        ApiMessage msg = null;
        ApiUtils.checkUserNameToken(dto);
        msg = new ApiMessage("200" , "现金账单" );
        User user = servUser.getUserByName(dto.getString("userName"));
        if (ApiUtils.objIsEmpty(user)) {
            throw new ApiException("102", "用户不存在");
        }
        Integer pageIndex = dto.getInteger("page") == null ? 1 : dto.getInteger("page");
        Integer pageSize = dto.getInteger("pageSize") == null ? 10 : dto.getInteger("pageSize");
        Pager pager = new Pager(pageIndex, pageSize);
        List<Map<String, Object>> list = veriService.getBillListMoney(pager, user.getId() );
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("token", dto.getString("token") );
        data.put("bills_list", list);
        data.put("count", pager.getRowCount() );
        data.put("total_page", pager.getPageCount() );
        data.put("current_page", pageIndex );
        msg.setData(Des3.encResponse(data));
        return msg;
    }

    @Override
    public ApiMessage billListCloud(JSONObject dto) throws ApiException {
        ApiMessage msg = null;
        ApiUtils.checkUserNameToken(dto);
        msg = new ApiMessage("200" , "商币账单" );
        User user = servUser.getUserByName(dto.getString("userName"));
        if (ApiUtils.objIsEmpty(user)) {
            throw new ApiException("102", "用户不存在");
        }
        Integer pageIndex = dto.getInteger("page") == null ? 1 : dto.getInteger("page");
        Integer pageSize = dto.getInteger("pageSize") == null ? 10 : dto.getInteger("pageSize");
        Pager pager = new Pager(pageIndex, pageSize);
        List<Map<String, Object>> list = veriService.getBillListCloud(pager, user.getId() );
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("token", dto.getString("token") );
        data.put("bills_list", list);
        data.put("count", pager.getRowCount() );
        data.put("total_page", pager.getPageCount() );
        data.put("current_page", pageIndex );
        msg.setData(Des3.encResponse(data));
        return msg;
    }
}
